package com.lanou.service;

import com.lanou.dao.InsertDao;
import com.lanou.dao.SelectDao;
import com.lanou.pojo.Book;
import com.lanou.pojo.Car;
import com.lanou.pojo.Tb_user;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class InsertService {
    private InsertDao insertDao = new InsertDao();
    private SelectDao selectDao = new SelectDao();
    //添加用户
    public int insertUser(Tb_user tbUser) {
        try {
            Long count = insertDao.selectUserByusername(tbUser);
            if (count!=0){
                //用户名已存在
                return 1002;
            }else {
                int insert = insertDao.insertUser(tbUser);
                if (insert!=0){
                    //创建成功
                    return 1000;
                }else {
                    //创建失败
                    return 1001;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1001;
    }
    //添加类
    public int insertCate(String cname) {
        try {
            Long catecount = insertDao.selectCateByCname(cname);
            if (catecount!=0){
                //种类名已存在
                return 1001;
            }else {
                int cate = insertDao.insertCate(cname);
                if (cate!=0){
                    //添加成功
                    return 1000;
                }else {
                    //添加失败
                    return 1002;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1002;
    }
    //修改种类
    public int changeCate(int cid, String cname) {
        try {
            Long catecount = insertDao.selectCateByCname(cname);
            if (catecount!=0){
                //种类名已存在
                return 1002;
            }else {
                int update = insertDao.changeCate(cid, cname);
                if (update!=0){
                    //修改成功
                    return 1000;
                }else {
                    //修改失败
                    return 1001;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1001;
    }
    //删除种类
    public int delCate(Integer cid, String cname) {
        try {
            int delCate = insertDao.delCate(cid, cname);
            Long delcount = insertDao.selectCateByCname(cname);
            if (delcount!=0){
                return 1001;
            }else {
                return 1000;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1001;
    }

    public int insertBook(String bname, String price, String author, String image, String cname) {
        try {
            Long aLong = insertDao.selectBookBybname(bname);
            if (aLong!=0){
                //书名已存在
                return 1001;
            }else {
                int cid = insertDao.selectCateIDBycname(cname);
                System.out.println(cid);
                System.out.println(bname);
                System.out.println(price);
                System.out.println(author);
                System.out.println(image);
                int insertbook = insertDao.insertBook(bname, price, author, image, cid);
                Long aLong1 = insertDao.selectBookBybname(bname);
                if (aLong1!=0){
                    //添加成功
                    return 1000;
                }else {
                    //添加失败
                    return 1002;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1002;
    }

    public boolean delBook(int bid) {
        try {
            int delBook = insertDao.delBook(bid);
            Long book = insertDao.selectBookcountBybid(bid);
            if (book!=0){
                return false;
            }else {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    //修改图书
    public int changeBook(int bid, String bname, String price, String author, String cate) {
        try {
            Long aLong = insertDao.selectBookBybname(bname);
            if (aLong!=0){
                //图书名已存在
                return 1001;
            }else {
                int cateIDBycname = insertDao.selectCateIDBycname(cate);
                int i = insertDao.changeBook(bid, bname, price, author, cateIDBycname);
                if (i!=0){
                    return 1000;
                }else {
                    //修改失败
                    return 1002;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1002;
    }
//  创建购物车
    public boolean insertCar(int uid, int bid) {
        try {
            Book book = insertDao.selectBookBybid(bid);
            Long carcount = insertDao.selectCarByBidAndUid(uid, bid);
            System.out.println(carcount);
            if (carcount==0){
                int update = insertDao.insertCar(uid, bid, book);
                if (update!=0){
                    return true;
                }else {
                    System.out.println("失败了?");
                    return false;
                }
            }else if (carcount==1){
                int carbookcount = insertDao.selectCarCountBybidAnduid(bid, uid);
//                carbookcount = carbookcount+1;
                System.out.println("carbookcount:"+carbookcount);
                int changeCar = insertDao.changeCar(uid, bid, carbookcount+1, book);
                System.out.println("changeCar:"+changeCar);
                if (changeCar!=0){
                    return true;
                }else {
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
// 修改购物车
    public boolean changeCar(int uid, int bid, int count, double price) {
        try {
            int carByhtml = insertDao.changeCarByhtml(uid, bid, count, price);
            if (carByhtml!=0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
//  删除单条购物车
    public boolean delCarOne(int carid) {
        try {
            int delCarOne = insertDao.delCarOne(carid);
            if (delCarOne!=0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
//  创建订单
    public int insertOrder(int uid, double total) {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
//        System.out.println(dateFormat.format(date));
        String ordertime = dateFormat.format(date);
        String address=null;
        try {
            address = selectDao.selectAddressBYuid(uid);
            int oid = insertDao.insertOrders(ordertime, total, uid, address);
            Long carcount = selectDao.selectCarcount(uid);
            if (carcount>0){
                List<Car> cars = selectDao.selectCarByuid(uid);
                for (Car car : cars) {
                    int count = car.getCount();
                    double subtotal = car.getTotal();
                    int bid = car.getBid();
                    int update = insertDao.insertOderitem(count, subtotal, oid, bid);
                    if (update>0){
                        insertDao.delCarOne(car.getCarid());
                    }
                }
                Long carcount1 = selectDao.selectCarcountagain(uid);
                System.out.println(carcount1);
                //if (carcount1==0){
                    //添加成功
                    return 1000;
                //}else {
                    //添加失败
                   // return 1003;
                //}
            }else {
                //购物车为空天加
             return 1001;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1003;
    }
    //修改订单状态
    public boolean changeOrdersState(int oid, int newstate) {
        try {
            int update = insertDao.changeOrdersstateByoid(oid, newstate);
            if (update!=0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
        //删除订单
    public boolean delOrders(int oid) {
        try {
            int delorderitem = insertDao.delorderitem(oid);
            int delorders = insertDao.delorders(oid);
            if (delorderitem!=0&&delorders!=0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    //修改用户
    public boolean changeUser(int uid, String email, String address, String phone) {
        try {
            int update = insertDao.changeUser(uid, email, address, phone);
            if (update!=0){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
        //修改密码
    public int changePassword(int uid, String password, String newpassword) {
        try {
            Tb_user tbUser = selectDao.selectAllUserByuid(uid);
            if (tbUser.getPassword()!=password){
                return 1005;
            }else {
                int update = insertDao.changeUserPassByuid(uid, newpassword);
                if (update!=0){
                    return 1000;
                }else {
                    return 1001;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 1001;
    }
}
